package com.shensi.appCollect.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.shensi.appCollect.dao.AppUserInfoMapper;
import com.shensi.appCollect.pojo.AppUserInfo;
import com.shensi.appCollect.pojo.dto.CardPasswordDto;
import com.shensi.appCollect.service.ICardPasswordService;
import com.shensi.appCollect.utils.DESUtils;
import com.shensi.appCollect.utils.exception.CustomException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service("cardPasswordService")
@Slf4j
public class CardPasswordServiceImpl implements ICardPasswordService {

    @Resource
    private AppUserInfoMapper appUserInfoMapper;

    @Override
    public CardPasswordDto selectCardPassword(Integer enterpriseId, String mobilePhone) {

        if(Objects.isNull(enterpriseId) || StringUtils.isEmpty(mobilePhone)){
            throw new CustomException("selectCardPassword请求参数异常");
        }
        AppUserInfo appUserInfo = new AppUserInfo();
        appUserInfo.setStationId(enterpriseId);
        appUserInfo.setMobilePhone(mobilePhone);
        List<AppUserInfo> appUserInfos = appUserInfoMapper.getAppUserInfoByMobilePhoneAndStationId(appUserInfo);

        if(CollectionUtils.isEmpty(appUserInfos)){
            log.info("selectCardPassword 找不到会员信息 enterpriseId:[{}], mobilePhone:[{}]",enterpriseId,mobilePhone);
            throw new CustomException("selectCardPassword 找不到会员信息");
        }
        AppUserInfo newAppUserInfo = appUserInfos.get(0);
        List<Integer> userIds = appUserInfos.stream().map(AppUserInfo::getUserId).collect(Collectors.toList());

        CardPasswordDto cardPasswordDto = new CardPasswordDto();
        cardPasswordDto.setEnterpriseId(enterpriseId);
        cardPasswordDto.setMobilePhone(mobilePhone);
        cardPasswordDto.setIds(userIds);
        cardPasswordDto.setIsFree(newAppUserInfo.getIsFree());
        cardPasswordDto.setPayPassword(DESUtils.decrypt(newAppUserInfo.getPayPassword()));
        return cardPasswordDto;
    }

    @Override
    public void updateCardPassword(CardPasswordDto cardPasswordDto) {
        if(Objects.isNull(cardPasswordDto)){
            throw new CustomException("updateCardPassword请求参数异常");
        }
        for(Integer id : cardPasswordDto.getIds()){
            AppUserInfo appUserInfo = new AppUserInfo();
            appUserInfo.setIsFree(cardPasswordDto.getIsFree());
            appUserInfo.setPayPassword(DESUtils.encrypt(cardPasswordDto.getPayPassword()));
            appUserInfo.setUserId(id);
            int num = appUserInfoMapper.updatePasswordById(appUserInfo);
            if(num<=0){
                log.info("updateCardPassword电子卡修改密码失败：[{}]", JSONObject.toJSONString(cardPasswordDto));
            }
        }
    }

    @Override
    public Boolean checkCardPassword(CardPasswordDto cardPasswordDto) {
        if(Objects.isNull(cardPasswordDto) || CollectionUtils.isEmpty(cardPasswordDto.getIds())
                || StringUtils.isEmpty(cardPasswordDto.getPayPassword())){
            throw new CustomException("checkCardPassword请求参数异常");
        }
        // 同一个人同一油企下 电子卡密码一致
        Integer userId = cardPasswordDto.getIds().get(0);
        // 密码校验
        AppUserInfo appUserInfo = appUserInfoMapper.getAppUserById(userId.longValue());
        String inputPassword = DESUtils.encrypt(cardPasswordDto.getPayPassword());
        if(inputPassword.equals(appUserInfo.getPayPassword())){
            return Boolean.TRUE;
        }else {
            return Boolean.FALSE;
        }
    }


}
